using System;
using System.Collections.Generic;
using System.Linq;
using MovieDB.Common;
using MovieDB.DTOs;
using MovieDB.ServiceInterfaces;

namespace MovieDB.Services
{
    public class CategoryService : ICategoryService
    {
        #region ICategoryService Members

        public CategoryDto GetMovieCategories()
        {
            var categoryDto = new CategoryDto
                                  {
                                      Genre = CreateCategoryItems<MovieGenre>(),
                                      VideoTypes = CreateCategoryItems<VideoType>()
                                  };

            return categoryDto;
        }

        #endregion

        private IEnumerable<CategoryItem<T>> CreateCategoryItems<T>() where T : struct
        {
            return Enum.GetValues(typeof (T)).Cast<Enum>()
                .Where(enumValue => ((int)(object)enumValue) != -1)
                .Select(enumValue => new CategoryItem<T>
                                          {
                                              Enum = (T) (object) enumValue,
                                              EnumDescription = enumValue.GetEnumDescription()
                                          }).OrderBy(categoryItem => categoryItem.EnumDescription);
        }
    }
}